package org.ccu.eshop.dao;

import org.ccu.eshop.domain.dto.CategoryVO;
import org.ccu.eshop.domain.dto.MemberVo;
import org.ccu.eshop.utils.ComboPooledDataSourceUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MemberDao {
    /**
     * 按照用户名和密码查询用户信息
     * @param dto
     * @return
     */
    public MemberVo selectMemberByLoginNameAndPwd(MemberVo dto) {
        MemberVo vo=null;
        //1-获得数据库的连接
        Connection connection= ComboPooledDataSourceUtil.getConnection();
        //2-操作数据库表
        try{

            //2-1获得承接sql的容器对象
            String sql="SELECT member.id,memberLevel,loginName, " +
                    "loginPwd,memberName,phone,address,zip,DATE_FORMAT(regDate,'%Y-%m-%d') regDate, " +
                    "DATE_FORMAT(lastDate,'%Y-%m-%d') lastDate,loginTimes,email,memberLevel.levelName, " +
                    "memberLevel.favourable from member,memberlevel " +
                    "WHERE member.memberLevel=memberLevel.id and loginName=? and loginPwd=?";
            //2-2获得sql语句
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1,dto.getLoginName());
            ps.setString(2,dto.getLoginPwd());
            //2-3执行sql语句
            ResultSet set=ps.executeQuery();
           if (set.next()){
                vo=new MemberVo(set.getInt("id"),set.getInt("memberLevel"),set.getString("loginName"),set.getString("loginPwd"),
                        set.getString("memberName"),set.getString("phone"),set.getString("address"),set.getString("zip"),set.getString("regDate"),set.getString("lastDate"),
                        set.getInt("loginTimes"),set.getString("email"),set.getString("levelName"),set.getInt("favourable"));

                }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //3关闭数据库连接
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return vo;
    }
}
